import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MaterialApp(home: TextHomePage()));
}

class TextHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TextHomePageState();
  }
}

class TextHomePageState extends State {
  ScrollController controller = ScrollController(initialScrollOffset: 400);
  bool _istrue = false;
  @override
  void initState() {
    // TODO: implement initState
    controller.addListener(() {
      print("监听到滚动.....");
      print("滚动位置${controller.offset}");
      setState(() {
        _istrue = controller.offset >=30;
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    /**
     * sliver
     *
     *
     *
     * 滚动的监听
     *  controller
     *  NatificationListener
     */
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("listview"),
        ),
        body:
        NotificationListener(
          onNotification: (ScrollNotification notification){
            if(notification is ScrollNotification){
              print("开始滚动");
            }else if(notification is ScrollUpdateNotification){
              print("正在滚动");
            }else if(notification is ScrollEndNotification){
              print("结束滚动");
            }
            return true;
          },
          child: ListView.builder(
              controller: controller,
              itemCount: 100,
              itemBuilder: (BuildContext context,int index){
                return ListTile(
                  leading: Icon(Icons.accessibility),
                  title: Text("test"),
                );
              }),
        ),
        floatingActionButton: _istrue?FloatingActionButton(
          child: Icon(Icons.arrow_upward),
          onPressed: (){
            controller.animateTo(0, duration: Duration(seconds: 1), curve: Curves.bounceIn);
          },
        ):null,
      ),
    );
  }
}
